package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyOrderActivity extends BaseActivity {

    /**
     * this activity context
     */
    private Context mContext;

    /**
     * tabs标题
     */
    private String[] mTabTitles = {"全部", "待发货", "待收货", "待使用","已使用"};

    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void init() {
        mContext = this;
        initListener();
        initTabLayout();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    private void initListener() {
        // 返回
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initTabLayout() {
        String uid = UserInfoBean.getUid(mContext);
        final ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MyOrderFragment.newInstance(uid, 0));
        fragments.add(MyOrderFragment.newInstance(uid, 2));
        fragments.add(MyOrderFragment.newInstance(uid, 3));
        fragments.add(MyOrderFragment.newInstance(uid, 1));
        fragments.add(MyOrderFragment.newInstance(uid, 4));
        Intent intent = getIntent();
        int type = intent.getIntExtra("type", 0);

        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTabTitles[position];
            }
        });
        mViewPager.setCurrentItem(type);
        mTabLayout.setViewPager(mViewPager);
    }
}
